import axios from 'axios'
import qs from 'qs'

axios.defaults.baseURL = '/proxy_url'        //关键代码
//添加一个请求拦截器
axios.interceptors.request.use(
    config =>{
        //设置post请求的请求头部信息
        if (config.method == 'post') {
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
            config.data = qs.stringify(config.data);
        }
        const token = window.sessionStorage.getItem('token');
        if (token) {
            config.headers.Authorization = token;
            return config;
        }
        return config;
    },
    error =>{
        return Promise.reject(error);
    }
)

export default axios //导出axios
